<?php
namespace app\repair\controller;
use app\common\controller\Common;
use app\common\model\Supplier;
use think\Request;
class Goods extends Common
{
	protected $order = 'goods_id desc';
	protected $keyword_search = [];
	protected $model = null;
	protected $afterActionList = [
        'addOperLog'  =>  ['only'=>'purchase'],
    ];
    
	public function _initialize()
	{
		parent::_initialize();
	}

	/**
	 * 增加维修配件页面
	 */
	public function change_goods($ty = null)
	{
		if(Request()->isGet()){
			$this->model = model('RepairGoods');
			$this->assign('cate',$this->model->getRepairCat());
			if($ty == 'edit'){
				return $this->fetch('edit_change_goods');
			}
			return $this->fetch('');
		}
	}
	/**
	 * 总部
	 * 所有维修配件统计
	 * 完成的维修订单数 倒叙排
	 */
	public function parts_count()
	{
		$this->model = model('RepairGoods');
		if(Request()->isGet()){
			if(Request()->isAjax()){
				$data = Request()->param('');
				$page = isset($data['page']) ? $data['page'] : 1;
				$row = isset($data['limit']) ? $data['limit'] : $this->row;
				
				$where = $this->handleParts($data);

				$list = $this->model->partsList($where['goods'], $where['order'],$data['order_name'], $page, $row);
				$list = collection($list)->toArray();
				$count = $this->model->partsCount($where['goods'], $data['order_name']);
				return json(['code'=>0,'msg'=>config('status.SUCCESS_ONE'),'count'=>$count,'data'=>$list,'page'=>$page,'row'=>$row]);
			}
			$shop = $this->model->getShop();
			$this->assign('shop',$shop);
			return $this->fetch();
		}
	}
	/**
	 * 总部
	 * 所有维修配件统计 条件处理
	 * @param  array $data 条件参数
	 */
	private function handleParts($data)
	{
		$order = [];//订单筛选条件
		$goods = [];//配件筛选条件
		/*@订单*/
		/*时间*/
		if(isset($data['ty']) && $data['ty'] != ''){
			switch($data['ty']){
				case 'now': $order['r.addtime'] = ['between',$this->activeTime()];break;
				case 'pre': $order['r.addtime'] = ['between',$this->preTime()];break;
				case 'cus': if($cusTime = $this->cusTime($data)){$order['r.addtime'] = $cusTime;};break;
			}
		}
		/*@配件*/
		/*所有门店*/
		if(isset($data['shop_id']) && $data['shop_id'] != ''){
			$goods['g.shop_id'] = $data['shop_id'];
		}
		return ['order'=>$order,'goods'=>$goods];
	}
	/**
	 * 获取选择的维修配件
	 */
	public function repair_goods($ty = null)
	{
		if(Request()->isAjax()){
			$this->order = 'repair_goods_id desc';
			$data = Request()->param('');
			$this->model = model('RepairGoods');
			
			$page = isset($data['page']) ? $data['page'] : 1;
			$row = isset($data['limit']) ? $data['limit'] : $this->row;
			$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
			$search = $this->keyword_search($search_arr);
			$order = $this->order;
			/*@条件处理*/
			if(isset($data['repair_id']) && $data['repair_id'] != ''){
				$where['repair_id'] = $data['repair_id'];
			}else{
				$where = false;
			}
			// print_r($ty);
			$list = $this->model->listData($where, $order, $page, $row, $search, $ty);
			$list = collection($list)->toArray();
			$count = $this->model->listCount($where, $order, $search, $ty);
			return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $list, $page, $row);
		}
	}
	/**
	 * 获取所有维修配件
	 */
	public function allGoods($ty = null)
	{
		if(Request()->isAjax()){
			$this->keyword_search = ['g.goods_name','g.goods_id','g.goods_sn','g.brand_name','g.memory_name','g.attribute_name','g.colour_name','g.marker'];
			$this->order = 'g.goods_id desc';
			$data = Request()->param('');
			$this->model = model('Goods');

			// return $this->getList($data);

			$page = isset($data['page']) ? $data['page'] : 1;
			$row = isset($data['limit']) ? $data['limit'] : $this->row;
			$search_arr = isset($data['keyword']) ? $data['keyword'] : '';
			$search = $this->keyword_search($search_arr);
			$order = $this->order;
			$where = $this->handleList($data);
			
			$list = $this->model->listData($where, $order, $page, $row, $search, $ty);
			// echo $this->model->getlastsql();
			$list = collection($list)->toArray();
			$count = $this->model->listCount($where, $order, $search, $ty);
			return $this->jsonList(0, config('status.SUCCESS_ONE'), $count, $list, $page, $row);
		}
	}
	/**
	 * 缺货采购
	 */
	public function purchase()
	{
		if(Request()->isGet()){
			$this->assign('supplier',(new Supplier())->supplierList(['is_show'=>0],'supplier_id,supplier_name'));
			return $this->fetch('');
		}
		if(Request()->isPost()){
			$supplier = new \app\repair\model\PurchaseOrder;
			$data = Request()->param('');
			$data['admin_id'] = session('admin.admin_id');
			$data['shop_id'] = session('admin.shop_id');
	        $data['state'] = 1;
			$data['addtime'] = time();
			try{
				$supplier->allowField(true)->save($data);
				$this->success(config('status.SUCCESS_ONE'));
			}catch(Exception $e){
				$this->error(config('status.ERROR_FIVE'));
			}
		}
	}
	/************************************ 私有 ***************************************/
	protected function handleList($data)
	{
		$where = [];
		$where['g.is_show'] = 0;
		$where['g.shop_id'] = session('admin.shop_id');
		/*@子分类*/
		if(isset($data['cat_id']) && $data['cat_id'] != ''){
			$where['g.cat_id'] = $data['cat_id'];
		}
		/*@分类-维修配件id*/
		$where['g.category_id'] = config('parameter.repair_cat_id');
		return $where;
	}
}